/*! ******************************************************************************
 *
 * Pentaho
 *
 * Copyright (C) 2024 by Hitachi Vantara, LLC : http://www.pentaho.com
 *
 * Use of this software is governed by the Business Source License included
 * in the LICENSE.TXT file.
 *
 * Change Date: 2029-07-20
 ******************************************************************************/


package org.pentaho.di.trans.steps.dimensionlookup;

import org.junit.Before;
import org.junit.Test;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepPartitioningMeta;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class DimensionLookupTest {
  private DatabaseMeta databaseMeta;
  private DimensionLookup dimensionLookupSpy;

  @Before
  public void setUp() throws Exception {
    databaseMeta = mock( DatabaseMeta.class );
    doReturn( "" ).when( databaseMeta ).quoteField( anyString() );

    DimensionLookupMeta dimensionLookupMeta = mock( DimensionLookupMeta.class );
    doReturn( databaseMeta ).when( dimensionLookupMeta ).getDatabaseMeta();
    doReturn( new String[]{} ).when( dimensionLookupMeta ).getKeyLookup();
    doReturn( new String[]{} ).when( dimensionLookupMeta ).getFieldLookup();
    doReturn( new int[]{} ).when( dimensionLookupMeta ).getFieldUpdate();

    StepMeta stepMeta = mock( StepMeta.class );
    doReturn( "step" ).when( stepMeta ).getName();
    doReturn( mock( StepPartitioningMeta.class ) ).when( stepMeta ).getTargetStepPartitioningMeta();
    doReturn( dimensionLookupMeta ).when( stepMeta ).getStepMetaInterface();

    Database db = mock( Database.class );
    doReturn( mock( Connection.class ) ).when( db ).getConnection();

    DimensionLookupData dimensionLookupData = mock( DimensionLookupData.class );
    dimensionLookupData.db = db;
    dimensionLookupData.keynrs = new int[] { };
    dimensionLookupData.fieldnrs = new int[] { };

    TransMeta transMeta = mock( TransMeta.class );
    doReturn( stepMeta ).when( transMeta ).findStep( anyString() );

    DimensionLookup dimensionLookup =
      new DimensionLookup( stepMeta, dimensionLookupData, 1, transMeta, mock( Trans.class ) );
    dimensionLookup.setData( dimensionLookupData );
    dimensionLookup.setMeta( dimensionLookupMeta );
    dimensionLookupSpy = spy( dimensionLookup );
    doReturn( stepMeta ).when( dimensionLookupSpy ).getStepMeta();
    doReturn( false ).when( dimensionLookupSpy ).isRowLevel();
    doReturn( false ).when( dimensionLookupSpy ).isDebug();
    doReturn( true ).when( dimensionLookupSpy ).isAutoIncrement();
    doNothing().when( dimensionLookupSpy ).logDetailed( anyString() );
  }

  @Test
  public void testDimInsert() throws Exception {
    RowMetaInterface rowMetaInterface = mock( RowMetaInterface.class );
    Object[] objects = mock( List.class ).toArray();
    Date date = mock( Date.class );
    dimensionLookupSpy.dimInsert( rowMetaInterface, objects, 132323L, true, null, date, date );
    verify( databaseMeta, times( 0 ) ).supportsAutoGeneratedKeys();

    dimensionLookupSpy.dimInsert( rowMetaInterface, objects, null, true, null, date, date );
    verify( databaseMeta, times( 2 ) ).supportsAutoGeneratedKeys();
  }
}
